const API = require('../../../../utils/api')
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        industryIndex: 0,
        industry:[],
        form: {
            "id": "",
            "cardId": "",
            "parkIntroduceImg": "", //园区介绍图片
            "companyProfile": "",   //企业介绍
            "location": "", //地理位置
            "ambitus": "",  //位置周边/交通
            "publicityVideo": "",   //宣传片
            "honorCertification": "",   //荣誉资质
            "honorCertificationArr": [],   //荣誉资质
            "brochure": "", //宣传册
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            industry: app.globalData.industry,
            ['form.cardId']: options.cardId,
        })
        this.get_park_detail(options.id)
    },

    /**
     * 表单提交
     */
    submit:function(e){
        let form = this.data.form
        form.ambitus = e.detail.value.ambitus
        form.companyProfile = e.detail.value.companyProfile
        form.companylocationProfile = e.detail.value.location
        form.honorCertificationArr.length > 0 ? form.honorCertification = form.honorCertificationArr.join(',') : ''
        // console.log( form )

        //表单验证
        if( !form.parkIntroduceImg ){
            wx.showToast({
              title: '请上传企业照片',
              icon:'none'
            })
            return
        }
        if( !form.companyProfile ){
            wx.showToast({
              title: '请填写企业介绍',
              icon:'none'
            })
            return
        }
        if( !form.location ){
            wx.showToast({
              title: '请填写企业地址',
              icon:'none'
            })
            return
        }
        if( !form.ambitus ){
            wx.showToast({
              title: '请填写交通信息',
              icon:'none'
            })
            return
        }

        wx.showLoading({
          title: '提交中',
        })
        API.update_park_info( form ).then( res => {
            if( res.code == 200 ){
                wx.showToast({
                  title: '提交成功',
                })
                setTimeout(() => {
                    wx.hideLoading()
                    wx.navigateBack({
                        delta: 1
                    })
                }, 1500);
            }else{
                wx.hideLoading()
                wx.showToast({
                  title: res.msg,
                  icon:'none'
                })
            }
        })
    },

    /**
     * 接口 - 获取园区详情
     */
    get_park_detail:function(id){
        let that = this
        const userInfo = wx.getStorageSync('userInfo')
        API.get_park_detail({
            id: id,
            wxUserid:userInfo.id,
            // id: 29,
            // wxUserid:103,
            sourse: 1,
        }).then( res => {
            if( res.code == 200 ){
                let form = that.data.form
                form.id = res.data.id
                form.parkIntroduceImg = res.data.parkIntroduceImg
                form.companyProfile = res.data.text
                form.location = res.data.location
                form.ambitus = res.data.ambitus

                form.publicityVideo = res.data.publicityVideo
                res.data.honorCertification ? form.honorCertification = res.data.honorCertification : ''
                res.data.honorCertification ? form.honorCertificationArr = res.data.honorCertification.split(',') : ''
                res.data.brochure ? form.brochure = res.data.brochure : ''
                console.log('详情', form)
                that.setData({
                    form: form
                })
            }
        })
    },

    /**
     * 事件 - 行业选择
     */
    industryChange:function(e){
        this.setData({
            industryIndex: parseInt(e.detail.value)
        })
    },

    /**
     * 企业照片 ----------------------------------------------------------------------------------------
     */

    /**
     * 图片上传 - 企业照片
     */
    uploadParkIntroduceImg:function(){
        let that = this
        app.uploadImg(1, callback)
        function callback(data){
            console.log(data)
            that.setData({
                ['form.parkIntroduceImg']: data
            })
        }
    },

    /**
     * 图片删除 - 企业照片
     */
    deleteParkIntroduceImg:function(){
        this.setData({
            ['form.parkIntroduceImg']: ''
        })
    },

    /**
     * 宣传片 --------------------------------------------------------------------------------------------
     */

    /**
     * 文件上传 - 宣传片
     */
    uploadPublicityVideo:function(){
        let that = this
        app.uploadVideo(1, callback)
        function callback(data){
            console.log(data)
            that.setData({
                ['form.publicityVideo']: data
            })
        }
    },

    /**
     * 文件删除 - 宣传片
     */
    deletePublicityVideo:function(){
        this.setData({
            ['form.publicityVideo']: ''
        })
    },

    /**
     * 荣誉照片 ----------------------------------------------------------------------------------------
     */

    /**
     * 图片上传 - 荣誉照片
     */
    uploadHonorCertification:function(){
        let that = this
        app.uploadImg(1, callback)
        function callback(data){
            console.log(data)
            that.setData({
                ['form.honorCertificationArr']: [...that.data.form.honorCertificationArr,...[data]]
            })
        }
    },

    /**
     * 图片删除 - 荣誉照片
     */
    deleteHonorCertification:function(e){
        let index = e.currentTarget.id
        let honorCertification = this.data.form.honorCertificationArr
        honorCertification.splice(index, 1)
        this.setData({
            ['form.honorCertificationArr']: honorCertification
        })
    },

    /**
     * 宣传册 ----------------------------------------------------------------------------------------
     */

    /**
     * 文件上传 - 宣传册
     */
    uploadBrochure:function(){
        let that = this
        app.uploadFile(1, callback)
        function callback(data){
            console.log(data)
            that.setData({
                ['form.brochure']: data
            })
        }
    },

    /**
     * 文件删除 - 宣传册
     */
    deleteBrochure:function(){
        this.setData({
            ['form.honorCertification']: ''
        })
    },
})